package com.xiaobao.campusLostFoundAfter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaobao.campusLostFoundAfter.common.Result;
import com.xiaobao.campusLostFoundAfter.mapper.UserMapper;
import com.xiaobao.campusLostFoundAfter.pojo.User;
import com.xiaobao.campusLostFoundAfter.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    // 添加用户
    @Override
    public Result<String> addUser(User user) {
        return userMapper.addUser(user) > 0 ? Result.success() : Result.error("添加用户失败");
    }

    // 获取所有用户
    @Override
    public Result<List<User>> getAllUsers() {
        return Result.success(userMapper.getAllUsers());
    }

    // 根据id获取用户
    @Override
    public Result<User> getUserById(String userId) {
        return Result.success(userMapper.getUserById(userId));
    }

    // 删除用户
    @Override
    public Result<String> deleteUser(String userId) {
        return userMapper.deleteUser(userId) > 0 ? Result.success() : Result.error("删除用户失败");
    }

    // 更新用户
    @Override
    public Result<String> updateUser(User user) {
        return userMapper.updateUser(user) > 0 ? Result.success() : Result.error("更新用户失败");
    }

    // 更新用户头像
    @Override
    public void updateAvatarUrl(String userId, String avatarUrl) {
        userMapper.updateAvatarUrl(userId, avatarUrl);
    }

    @Override
    public Result<PageInfo<User>> getAllUsersByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.getAllUsersByPage();
        return Result.success(new PageInfo<>(users));
    }
}
